package com.zyb.patient.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.View;



public class StringUtil
{
	public static String subString(String text, int length, String endWith)
	{
		int textLength = text.length();
		int byteLength = 0;
		StringBuffer returnStr = new StringBuffer();
		for (int i = 0; i < textLength && byteLength < length * 2; i++)
		{
			String str_i = text.substring(i , i + 1);
			if (str_i.getBytes().length == 1)
			{
				byteLength++;
			}
			else
			{
				byteLength += 2;
			}
			returnStr.append(str_i);
		}
		try
		{
			if (byteLength < text.getBytes("GBK").length)
			{
				returnStr.append(endWith);
			}
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		return returnStr.toString();
	}
	
	
	
	/**
	 * 弹出对话框
	 * 
	 * @param 
	 * @return
	 */
	public static void showDialog2(Context context,String content,OnClickListener onPosClick){
		AlertDialog.Builder builder = new AlertDialog.Builder(
				context);
		builder.setMessage(content);
		builder.setCancelable(false);
		builder.setPositiveButton("确定", onPosClick);
		builder.show();
	}
	
	
	public static File createSDDir(String dirName)
	{
		File dir = new File( Environment.getExternalStorageDirectory()+"/" + dirName);
		dir.mkdirs();
		return dir;
	}

	// 判断SD卡上的文件夹是否存在
	public static boolean isFileExist(String fileName)
	{
		File file = new File(Environment.getExternalStorageDirectory()+"/" + fileName);
		return file.exists();
	}
	
	public static Bitmap getViewBitmap(View v) {
        v.clearFocus(); // 清除视图焦点
        v.setPressed(false);// 将视图设为不可点击

        boolean willNotCache = v.willNotCacheDrawing(); // 返回视图是否可以保存他的画图缓存
        v.setWillNotCacheDrawing(false);

     //将视图在此操作时置为透明
        int color = v.getDrawingCacheBackgroundColor(); // 获得绘制缓存位图的背景颜色
        v.setDrawingCacheBackgroundColor(0); // 设置绘图背景颜色
        if (color != 0) { // 如果获得的背景不是黑色的则释放以前的绘图缓存
                v.destroyDrawingCache(); // 释放绘图资源所使用的缓存
        }
        v.buildDrawingCache(); // 重新创建绘图缓存，此时的背景色是黑色
        Bitmap cacheBitmap = v.getDrawingCache(); // 将绘图缓存得到的,注意这里得到的只是一个图像的引用
        if (cacheBitmap == null) {
                return null;
        }
        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // 将位图实例化
        //恢复视图
        v.destroyDrawingCache();// 释放位图内存
        v.setWillNotCacheDrawing(willNotCache);// 返回以前缓存设置
        v.setDrawingCacheBackgroundColor(color);// 返回以前的缓存颜色设置
         
        return bitmap;
}
	
	
	 public static String subZeroAndDot(String s){  
	        if(s.indexOf(".") > 0){  
	            s = s.replaceAll("0+?$", "");//去掉多余的0  
	            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉  
	        }  
	        return s;  
	    }  
	 

		public static Bitmap getPicFromBytes(byte[] bytes,
				BitmapFactory.Options opts) {
			if (bytes != null)
				if (opts != null)
					return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
							opts);
				else
					return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
			return null;
		}

		public static byte[] readStream(InputStream inStream) throws Exception {
			byte[] buffer = new byte[1024];
			int len = -1;
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			while ((len = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, len);
			}
			byte[] data = outStream.toByteArray();
			outStream.close();
			inStream.close();
			return data;

		}

		public static Bitmap getDiskBitmap(String pathString) {
			Bitmap bitmap = null;
			try {
				File file = new File(pathString);
				if (file.exists()) {
					bitmap = BitmapFactory.decodeFile(pathString);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

			return bitmap;
		}
		
	private final static String[] units = { "", "十", "百", "千", "万", "十万", "百万",
			"千万", "亿", "十亿", "百亿", "千亿", "万亿" };
	private final static char[] numArray = { '零', '一', '二', '三', '四', '五', '六',
			'七', '八', '九' };

	public static String foematInteger(int num) {
		char[] val = String.valueOf(num).toCharArray();
		int len = val.length;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < len; i++) {
			String m = val[i] + "";
			int n = Integer.valueOf(m);
			boolean isZero = n == 0;
			String unit = units[(len - 1) - i];
			if (isZero) {
//				if ('0' == val[i - 1]) {
//					// not need process if the last digital bits is 0
//					continue;
//				} else {
					// no unit for 0
					sb.append(numArray[n]);
//				}
			} else {
				sb.append(numArray[n]);
				sb.append(unit);
			}
		}
		return sb.toString();
	}
	 
}
